import Phaser from 'phaser'
import PlayerBase from "./PlayerBase";
import Director from './Director';

/**
 * Huonv player
 */
export default class PlayerHuonv extends PlayerBase {

    private actionLuoyan: SpineGameObject = null!;
    private huoqius: Phaser.GameObjects.Image[] = [];
    constructor(director: Director) {
        super(director, "sanim_224061");

        this.actionLuoyan = this.scene.add.spine(this.getX(), this.getY(), 'huonv_luoyan');
        this.actionLuoyan.scaleX = 0.3;
        this.actionLuoyan.scaleY = 0.3;
    }

    protected actionEvent(action: string, actionEventName: string) {
        if (action == 'att') {
            this.attack(actionEventName);
        }
        if (action == 'ultimateskill') {
            if (actionEventName != 'back') {
                this.ultimateskill(actionEventName);
            }
        }
        if (action == 'skill') {
            if (actionEventName != 'back') {
                this.skill(actionEventName);
            }
        }
        if (action == 'ultimateskill ') {
            if (actionEventName != 'back') {
                this.ultimateskill(actionEventName);
            }
        }
    }

    private skill(eventName: string): void {
        if (eventName == 'faqi_11') {
            let attachedTop = 30;
            let attachedRight = 300;
            let attachedLeft = -20;
            let attachedBottom = 30;
            if (this.directionX == 'left') {
                attachedLeft = 310;
            }

            let scope: number[] = this.attackScope(attachedLeft, attachedTop, attachedRight, attachedBottom, true);
            let players: PlayerBase[] = this.director.findAttachedPlayers(this, scope[0], scope[1], scope[2], scope[3]);
            if (players.length > 0) {
                players[0].beHit(0, this.skillOne.magicHurt, 1);
                this.actionLuoyan.x = players[0].spine.x;
                this.actionLuoyan.y = players[0].spine.y;
                this.actionLuoyan.play('animation_1');
            }
        }

    }

    private sendHuoqiu(player: PlayerBase): void {
        let sx = this.getX() + this.getWidth()
        let sy = this.getY()
        this.scene.anims.create({
            key: 'burning',
            frames: this.scene.anims.generateFrameNames('huoqiu'),
            frameRate: 12,
            repeat: -1
        });

        let huoqiu = this.scene.add.sprite(sx, sy, 'huoqiu');
        huoqiu.angle = -90;
        if (this.directionX == 'left') {
            huoqiu.angle = 90;
        }
        huoqiu.scaleX = 0.3;
        huoqiu.scaleY = 0.3;
        huoqiu.depth = 99999999;
        huoqiu.play('burning');
        this.scene.tweens.add({
            targets: huoqiu,
            x: player.spine.x,
            y: player.spine.y - (player.getHeight() / 2),
            scaleX: 1,
            scaleY: 1,
            duration: 300,
            ease: 'Power0',
            onComplete: () => {
                player.beHit(0, this.magicHurt, 1);
                huoqiu.destroy();
            }
        });
    }

    private attack(eventName: string): void {
        if (eventName == 'faqi_11') {
            // let backValue = 100;
            // let defaultX = this.getX() + 300;
            // let defaultY = this.getY() + 10;
            let attachedTop = 30;
            let attachedRight = 270;
            let attachedLeft = -20;
            let attachedBottom = 30;
            if (this.directionX == 'left') {
                // attachedRight = 270;
                attachedLeft = 290;
            }

            let scope: number[] = this.attackScope(attachedLeft, attachedTop, attachedRight, attachedBottom, true);
            let players: PlayerBase[] = this.director.findAttachedPlayers(this, scope[0], scope[1], scope[2], scope[3]);
            if (players.length > 0) {
                this.sendHuoqiu(players[0]);
            }
        }
    }

    private ultimateskill(eventName: string): void {
        let attachedTop = 40;
        let attachedRight = 650;
        let attachedLeft = -110;
        let attachedBottom = -20;
        let backValue = 100;
        if (this.directionX == 'left') {
            attachedLeft = 750;
            backValue = -100;
        }
        if (eventName == 'chufa_21_3_3') {
            let scope: number[] = this.attackScope(attachedLeft, attachedTop, attachedRight, attachedBottom, true);
            let players: PlayerBase[] = this.director.findAttachedPlayers(this, scope[0], scope[1], scope[2], scope[3]);
            if (players.length > 0) {
                players.forEach((player) => {
                    player.beHit(0, this.skillTwo.magicHurt, backValue);
                });
            }
        }
        if (eventName == 'chufa_21_3_2') {
            let scope: number[] = this.attackScope(attachedLeft, attachedTop, attachedRight, attachedBottom, true);
            let players: PlayerBase[] = this.director.findAttachedPlayers(this, scope[0], scope[1], scope[2], scope[3]);
            if (players.length > 0) {
                players.forEach((player) => {
                    player.beHit(this.skillTwo.magicHurt, backValue);
                });
            }
        }
        if (eventName == 'chufa_21_3_1') {
            let scope: number[] = this.attackScope(attachedLeft, attachedTop, attachedRight, attachedBottom, true);
            let players: PlayerBase[] = this.director.findAttachedPlayers(this, scope[0], scope[1], scope[2], scope[3]);
            if (players.length > 0) {
                players.forEach((player) => {
                    player.beHit(this.skillTwo.magicHurt, backValue);
                });
            }
        }

    }

}